package com.funong.matrix.biz.base.dao;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funong.matrix.biz.base.entity.SysPermissionEntity;
import com.funong.matrix.biz.base.mapper.SysPermissionMapper;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.enums.IsDeletedEnum;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

@Dao
@Aspect
public class SysPermissionDao extends FunongBaseDao<SysPermissionMapper, SysPermissionEntity> {

    public IPage<SysPermissionEntity> queryPermissionList(Page<SysPermissionEntity> page, SysPermissionEntity sysPermissionEntity) {
        return page(page, Wrappers.<SysPermissionEntity>lambdaQuery()
                .like(StringUtils.isNotEmpty(sysPermissionEntity.getCode()), SysPermissionEntity::getCode, sysPermissionEntity.getCode())
                .like(StringUtils.isNotEmpty(sysPermissionEntity.getName()), SysPermissionEntity::getName, sysPermissionEntity.getName())
                .eq(StringUtils.isNotEmpty(sysPermissionEntity.getQueryStatus()), SysPermissionEntity::getStatus, sysPermissionEntity.getQueryStatus())
                .eq(SysPermissionEntity::getSystemId, sysPermissionEntity.getSystemId())
                .eq(SysPermissionEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
                .orderByAsc(SysPermissionEntity::getOrderNum)
        );
    }

    public List<SysPermissionEntity> queryPermissionByCode(String code) {
        return list(Wrappers.<SysPermissionEntity>lambdaQuery()
                .eq(SysPermissionEntity::getCode, code)
                .eq(SysPermissionEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    public List<SysPermissionEntity> queryAllPermission() {
        return list(Wrappers.<SysPermissionEntity>lambdaQuery()
                .eq(SysPermissionEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    /**
     * 按编码获取
     *
     * @param code
     * @return
     */
    public SysPermissionEntity getByCode(String code) {
        return getOne(Wrappers.<SysPermissionEntity>lambdaQuery()
                .eq(SysPermissionEntity::getCode, code)
                .eq(SysPermissionEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue())
        );
    }

    /**
     * 下拉选择
     *
     * @param systemId
     * @param groupType
     * @return
     */
    public List<Tree<String>> querySelect(Integer systemId, Integer groupType) {
        List<SysPermissionEntity> list = this.list(
                SysPermissionEntity.gw()
                        .eq(SysPermissionEntity::getSystemId, systemId)
                        .and(groupType != null, w -> w
                                .isNull(SysPermissionEntity::getGroupType)
                                .or().eq(SysPermissionEntity::getGroupType, groupType))
                        .select(SysPermissionEntity::getCode, SysPermissionEntity::getName, SysPermissionEntity::getParentCode)
        );
        return TreeUtil.build(list, "0", (item, treeNode) -> treeNode.setParentId(item.getParentCode()).setId(item.getCode()).setName(item.getName()));
    }

    /**
     * 查询全部
     *
     * @param systemId
     * @return
     */
    public List<SysPermissionEntity> queryAll(Integer systemId) {
        return this.list(SysPermissionEntity.gw().eq(SysPermissionEntity::getSystemId, systemId));
    }
}